#!/bin/sh

# this script was intended to launch a command in a new gnome-terminal tab; that
# seems to be next to impossible;  currently it is only capable of creating a new
# tab
#
# it's pretty hokey, though;  the behaviour if multiple terminals are open is
# non-deterministic, also, it relies on external utilities (xdotool and
# wmctrl);  and also also, what happens if the command name of the terminal
# includes the text "grep"?

xdotool=`which xdotool`
wmctrl=`which wmctrl`

run_gterm ()
{
   exec gnome-terminal "$@"
   exit $?
}

if [ -z "$xdotool" ]
then
   echo "xdotool not found; please install it to enable reuse of gnome-terminal" >&2
   run_gterm "$@" # does not return
fi

if [ -z "$wmctrl" ]
then
   echo "wmctrl not found; please install it to enable reuse of gnome-terminal" >&2
   run_gterm "$@" # does not return
fi

if pgrep -lu "$USER" gnome-terminal | grep -v "grep" | grep -q "^[0-9]"
then
   WID=`$xdotool search --class "gnome-terminal" | head -1`
   if [ -n "$WID" ]
   then
      $xdotool windowfocus $WID &&
	 $xdotool key ctrl+shift+t &&
	 $wmctrl -i -a $WID &&
	 exit $?
   fi
fi

run_gterm "$@"

